package org.jeecg.modules.business.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.business.entity.BusMarketAgreementContacts;
import org.jeecg.modules.business.entity.BusMarketAgreementCustomer;
import org.jeecg.modules.business.entity.BusOrderSound;
import org.jeecg.modules.business.service.IBusMarketAgreementContactsService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 协议单位-联系人
 * @Author: jeecg-boot
 * @Date:   2023-04-11
 * @Version: V1.0
 */
@Api(tags="协议单位-联系人")
@RestController
@RequestMapping("/business/busMarketAgreementContacts")
@Slf4j
public class BusMarketAgreementContactsController extends JeecgController<BusMarketAgreementContacts, IBusMarketAgreementContactsService> {
	@Autowired
	private IBusMarketAgreementContactsService busMarketAgreementContactsService;

	/**
	 * 分页列表查询
	 *
	 * @param busMarketAgreementContacts
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "协议单位-联系人-分页列表查询")
	@ApiOperation(value="协议单位-联系人-分页列表查询", notes="协议单位-联系人-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BusMarketAgreementContacts>> queryPageList(BusMarketAgreementContacts busMarketAgreementContacts,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusMarketAgreementContacts> queryWrapper = QueryGenerator.initQueryWrapper(busMarketAgreementContacts, req.getParameterMap());
		Page<BusMarketAgreementContacts> page = new Page<BusMarketAgreementContacts>(pageNo, pageSize);
		IPage<BusMarketAgreementContacts> pageList = busMarketAgreementContactsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 列表查询
	  *
	  * @param busMarketAgreementContacts
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "协议单位-联系人-列表查询")
	 @ApiOperation(value="协议单位-联系人-列表查询", notes="协议单位-联系人-列表查询")
	 @GetMapping(value = "/queryList")
	 public Result<List<BusMarketAgreementContacts>> queryList(BusMarketAgreementContacts busMarketAgreementContacts, HttpServletRequest req) {
		 QueryWrapper<BusMarketAgreementContacts> queryWrapper = QueryGenerator.initQueryWrapper(busMarketAgreementContacts, req.getParameterMap());
		 LoginUser user = TokenUtils.getAuthUser();
		 if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
			 queryWrapper.eq("tenant_id",user.getRelTenantIds());
		 }
		 queryWrapper.eq("del_flag",0);
		 List<BusMarketAgreementContacts> list = busMarketAgreementContactsService.list(queryWrapper);
		 return Result.OK(list);
	 }

	/**
	 *   添加
	 *
	 * @param busMarketAgreementContacts
	 * @return
	 */
	@AutoLog(value = "协议单位-联系人-添加")
	@ApiOperation(value="协议单位-联系人-添加", notes="协议单位-联系人-添加")
	//@RequiresPermissions("business:bus_market_agreement_contacts_info:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BusMarketAgreementContacts busMarketAgreementContacts) {
		busMarketAgreementContacts.setDelFlag(CommonConstant.DEL_FLAG_0);
		if(busMarketAgreementContacts.getTenantId() == null || busMarketAgreementContacts.getTenantId().equals("")){
			LoginUser user = TokenUtils.getAuthUser();
			if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
				busMarketAgreementContacts.setTenantId(user.getRelTenantIds());
			} else {
				throw new JeecgBootException("当前登录人租户信息错误");
			}
		}
		//客户名称校验
		LambdaQueryWrapper<BusMarketAgreementContacts> queryWrapper = new LambdaQueryWrapper<BusMarketAgreementContacts>();
		queryWrapper.eq(BusMarketAgreementContacts::getName,busMarketAgreementContacts.getName());
		if (StringUtils.isNotBlank(busMarketAgreementContacts.getId())) {
			// 编辑页面校验
			queryWrapper.ne(BusMarketAgreementContacts::getId,busMarketAgreementContacts.getId());
		}
		long num = busMarketAgreementContactsService.count(queryWrapper);
		if (num != 0) {
			// 该值不可用
			return Result.error("客户名称已存在！");
		}
		busMarketAgreementContactsService.save(busMarketAgreementContacts);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param busMarketAgreementContacts
	 * @return
	 */
	@AutoLog(value = "协议单位-联系人-编辑")
	@ApiOperation(value="协议单位-联系人-编辑", notes="协议单位-联系人-编辑")
	//@RequiresPermissions("business:bus_market_agreement_contacts_info:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BusMarketAgreementContacts busMarketAgreementContacts) {
//		busMarketAgreementContactsService.updateById(busMarketAgreementContacts);
		if(busMarketAgreementContacts.getTenantId() == null || busMarketAgreementContacts.getTenantId().equals("")){
			LoginUser user = TokenUtils.getAuthUser();
			if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
				busMarketAgreementContacts.setTenantId(user.getRelTenantIds());
			} else {
				throw new JeecgBootException("当前登录人租户信息错误");
			}
		}
		//客户名称校验
		LambdaQueryWrapper<BusMarketAgreementContacts> queryWrapper = new LambdaQueryWrapper<BusMarketAgreementContacts>();
		queryWrapper.eq(BusMarketAgreementContacts::getName,busMarketAgreementContacts.getName());
		if (StringUtils.isNotBlank(busMarketAgreementContacts.getId())) {
			// 编辑页面校验
			queryWrapper.ne(BusMarketAgreementContacts::getId,busMarketAgreementContacts.getId());
		}
		long num = busMarketAgreementContactsService.count(queryWrapper);
		if (num != 0) {
			// 该值不可用
			return Result.error("客户名称已存在！");
		}
		BusMarketAgreementContacts editModel = busMarketAgreementContactsService.getById(busMarketAgreementContacts.getId());
		editModel.setName(busMarketAgreementContacts.getName());
		editModel.setSex(busMarketAgreementContacts.getSex());
		editModel.setBirthday(busMarketAgreementContacts.getBirthday());
		editModel.setCalls(busMarketAgreementContacts.getCalls());
		editModel.setDepartment(busMarketAgreementContacts.getDepartment());
		editModel.setCompanyPhone(busMarketAgreementContacts.getCompanyPhone());
		editModel.setPhone(busMarketAgreementContacts.getPhone());
		editModel.setHomePhone(busMarketAgreementContacts.getHomePhone());
		editModel.setPost(busMarketAgreementContacts.getPost());
		editModel.setPostalCode(busMarketAgreementContacts.getPostalCode());
		editModel.setEmail(busMarketAgreementContacts.getEmail());
		editModel.setAddress(busMarketAgreementContacts.getAddress());
		editModel.setHobby(busMarketAgreementContacts.getHobby());
		editModel.setRemarks(busMarketAgreementContacts.getRemarks());
		busMarketAgreementContactsService.updateById(editModel);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "协议单位-联系人-通过id删除")
	@ApiOperation(value="协议单位-联系人-通过id删除", notes="协议单位-联系人-通过id删除")
	//@RequiresPermissions("business:bus_market_agreement_contacts_info:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		busMarketAgreementContactsService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "协议单位-联系人-批量删除")
	@ApiOperation(value="协议单位-联系人-批量删除", notes="协议单位-联系人-批量删除")
	//@RequiresPermissions("business:bus_market_agreement_contacts_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busMarketAgreementContactsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "协议单位-联系人-通过id查询")
	@ApiOperation(value="协议单位-联系人-通过id查询", notes="协议单位-联系人-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BusMarketAgreementContacts> queryById(@RequestParam(name="id",required=true) String id) {
		BusMarketAgreementContacts busMarketAgreementContacts = busMarketAgreementContactsService.getById(id);
		if(busMarketAgreementContacts==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(busMarketAgreementContacts);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param busMarketAgreementContacts
    */
    //@RequiresPermissions("business:bus_market_agreement_contacts_info:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusMarketAgreementContacts busMarketAgreementContacts) {
        return super.exportXls(request, busMarketAgreementContacts, BusMarketAgreementContacts.class, "协议单位-联系人");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("business:bus_market_agreement_contacts_info:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusMarketAgreementContacts.class);
    }

}
